colorz
A zero-alloc, no_std
compatible way of coloring your terminal outputs!
This is a "fork" of owo-colors with more features.
use ;
// will print `hello world` in red
println!;
// will print `hello world` on a red background
println!;
// will print `hello world` on a with an Aqua underline
println!;
Features:
- Format using any
std
formatting trait, (Display
,Debug
, etc.) - Format using a custom format function (
StyledValue::fmt_with
) - Per-value conditional styling via
StyledValue::stream
- Global conditional styling for all
StyledValue
s viacolorz::mode
strip-colors
feature flag
- zero-dependency by default
- Standard names for Ansi, Xterm, and Css colors
- Rgb color support
- Ansi modifier (bold, italics, underline, etc.)
- Multi-color support (foreground, background, and underline color)
- mostly a drop-in replacement for
owo-colors
for simple cases- (some xterm color names may be different, some methods are called a little differently)
- compile-time selection of xterm colors by color code
- compile-time style construction
- compile-time style value construction
NO_COLOR
/ALWAYS_COLOR
environment variables:colorz::mode::{Mode::from_env, set_coloring_mode_from_env}
- requires
std
orsupports-color
feature
- requires
Feature Flags
This crate has a few feature flags
strip-colors
- removes all coloring forStyledValue
's formatting methodsstd
- this enables the standard library (since this library isno_std
by default)supports-color
- this enables thesupports-color
crate (which also uses thestd
library)
None of the feature is enabled by default. And they should only be turned on by the final binary crate.
If these features are turned off, then only the global mode settings is respected, and no stream-based color detection is done.
if strip-colors
is enabled, then colorz::mode::get_coloring_mode
will always
return Mode::Never
, and StyledValue
will never be colored.
else if supports-color
is enabled, then the supports-color
crate is used to detect if
ANSI, Xterm or RGB colors are supports. If a StyledValue
tries to use any unsupported
color types, then it will not do any coloring.
For example, if you terminal doesn't support Xterm colors, and you write
use ;
println!;
Then you will see hello world
in your default terminal color.
finally if std
is enabled, then if the stream is a terminal then all coloring types will be used.
and if the stream isn't a terminal then no coloring will be chosen.
Coloring Mode
There are many ways to specify the coloring mode for colorz
, and it may not be obvious how
they interact, so here is a precedence list. To figure out how colorz chooses to colorz, go
down the list, and the first element that applies will be selected.
- if the feature flag
strip-colors
is enabled -> NO COLOR - if the global coloring mode is
Mode::Always
-> DO COLOR - if the global coloring mode is
Mode::NEVER
-> NO COLOR - if the per-value stream if set to
Stream::AlwaysColor
-> DO COLORStream::NeverColor
-> NO COLORStream::Stdout
/Stream::Stderr
-> detect coloring usingstd
orsupport-color
(see docs on feature flags for details)
- if global stream is set to
Stream::AlwaysColor
-> DO COLORStream::NeverColor
-> NO COLORStream::Stdout
/Stream::Stderr
-> detect coloring usingstd
orsupport-color
(see docs on feature flags for details)
The global stream is always set to one of the possible Stream
values,
so one option on the list will always be chosen.
NOTE that setting the coloring mode from the environment sets the global coloring mode, so either the second or third option on the list.
NOTE that the coloring mode only affects StyledValue
(which includes all outputs of the Colorize
trait).
Using Style::apply
/Style::clear
directly will not respect the coloring mode, and can be used to force
coloring regardless of the current coloring mode. You can use Style::should_color
or mode::should_color
to detect if a given style
should be used based on the current coloring mode.
use ;
let style = new.fg;
if style.should_color
if style.should_color
Examples
Format any value
use ;
// will print `hello world` in red
println!;
// will print `100` on an aquamarine background
println!;
// will print `hello world` on a with an Aqua underline
println!;
With conditional formatting per value
use ;
// will print `hello world` in red if Stdout points to a terminal
println!;
Easily turn it off at any time
use ;
set_coloring_mode;
// doesn't style the value
println!;
assert_eq!;
Create compile time style sheets
use ;
const MY_STYLE: Style = new
.fg
.effects_array
.const_into_runtime_style;
// styles `my text` in forest green with italics and bold
println!;